package org.reliablesource.prism.gef.editor.parts;

import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editpolicies.GraphicalNodeEditPolicy;
import org.eclipse.gef.requests.CreateConnectionRequest;
import org.eclipse.gef.requests.ReconnectRequest;
import org.reliablesource.prism.gef.editor.commands.ConnectionCreateCommand;
import org.reliablesource.prism.gef.editor.commands.ConnectionReconnectCommand;
import org.reliablesource.prism.gef.editor.model.PointerLocation;
import org.reliablesource.prism.gef.editor.model.Wire;

/**
 *
 *  PointerLocationConnectionEditPolicy.java
 *  Copyright (c) 2006, Reliable Source, Inc. All Rights Reserved
 *
 *	Created on: Apr 24, 2006 
 *  @author Dennis Park <a href="mailto:dennis.park@gmail.com">dennis.park@gmail.com</a>
 *
 */

public class PointerLocationConnectionEditPolicy extends GraphicalNodeEditPolicy{

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.editpolicies.GraphicalNodeEditPolicy#getConnectionCompleteCommand(org.eclipse.gef.requests.CreateConnectionRequest)
	 */
	protected Command getConnectionCompleteCommand(
			CreateConnectionRequest request) {
		ConnectionCreateCommand cmd = (ConnectionCreateCommand) request
				.getStartCommand();
		cmd.setTarget((PointerLocation) getHost().getModel());
		return cmd;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.editpolicies.GraphicalNodeEditPolicy#getConnectionCreateCommand(org.eclipse.gef.requests.CreateConnectionRequest)
	 */
	protected Command getConnectionCreateCommand(
			CreateConnectionRequest request) {
		PointerLocation source = (PointerLocation) getHost()
				.getModel();
		int style = ((Integer) request.getNewObjectType())
				.intValue();
		ConnectionCreateCommand cmd = new ConnectionCreateCommand(
				source, style);
		request.setStartCommand(cmd);

		return cmd;
	}

	protected Command getReconnectSourceCommand(
			ReconnectRequest request) {
		Wire conn = (Wire) request.getConnectionEditPart()
				.getModel();
		PointerLocation newSource = (PointerLocation) getHost()
				.getModel();
		ConnectionReconnectCommand cmd = new ConnectionReconnectCommand(
				conn);
		cmd.setNewSource(newSource);
		return cmd;
	}

	protected Command getReconnectTargetCommand(
			ReconnectRequest request) {
		Wire conn = (Wire) request.getConnectionEditPart()
				.getModel();
		PointerLocation newTarget = (PointerLocation) getHost()
				.getModel();
		ConnectionReconnectCommand cmd = new ConnectionReconnectCommand(
				conn);
		cmd.setNewTarget(newTarget);
		return cmd;
	}


}
